package com.item.action;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpSession;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.base.action.CoreController;
import com.base.dao.model.Grid;
import com.base.dao.model.Ret;
import com.base.dialect.PaginationSupport;
import com.base.util.JSONUtils;
import com.base.web.annotation.LoginFilter;
import com.base.web.annotation.LoginMethod;
import com.item.dao.model.City;
import com.item.dao.model.CityExample;
import com.item.dao.model.Role;
import com.item.dao.model.UserGroup;
import com.item.service.CityService;
import com.item.util.RoleUtil;
import com.mdx.mobile.commons.verfy.Md5;

@RequestMapping("city")
@Controller
@LoginFilter
public class CityController extends CoreController{

    @Autowired
    private CityService cityService;
    
    @RequestMapping("/list")
	@ResponseBody 
    public String list(Integer page, Integer rows,String name,String flag){
    	CityExample example = new CityExample();
    	if(StringUtils.isNotBlank(name))example.createCriteria().andNameLike("%"+name+"%");
    	example.setOrderByClause("code");
    	if(StringUtils.isBlank(flag)){
	    	PaginationSupport.byPage(page, rows);
	    	List<City> list = cityService.selectByExample(example);
	      	return JSONUtils.serialize(new Grid(PaginationSupport.getContext().getTotalCount(), list));
    	}
    	List<City> list = cityService.selectByExample(example);
      	return JSONUtils.serialize(list);
    }
    
    @RequestMapping("/save")
	@ResponseBody 
    public String save(City city){
    	if(StringUtils.isEmpty(city.getCode())){
			city.setCreateTime(new Date());
			city.setRoleCode("cityAgent");
			city.setCode(cityService.getNewCodeByLevel());
			city.setPassword(Md5.mD5(city.getPassword()));
			cityService.insert(city);
		}else{
			if(StringUtils.isNotBlank(city.getPassword())){
				city.setPassword(Md5.mD5(city.getPassword()));
			}
			cityService.updateByPrimaryKeySelective(city);
		}
       	return JSONUtils.serialize(new Ret(0));
    }
    
    @RequestMapping("/findById")
	@ResponseBody 
    public String find(String id){
    	City city = cityService.selectByPrimaryKey(id);
       	return JSONUtils.serialize(city);
    }
    
    @RequestMapping("/del")
	@ResponseBody 
    public String del(String id){
    	String[] ids = id.split(",");
    	for (String str : ids){
    		if(str.equals("0"))continue;
    		cityService.deleteByPrimaryKey(str);
    	}
       	return JSONUtils.serialize(new Ret(0));
    }

	@RequestMapping("/checkAccount")
	@ResponseBody
	public String checkAccount(String value) throws Exception {
		CityExample example = new CityExample();
		example.createCriteria().andAccountEqualTo(value);
		int cnt = cityService.countByExample(example);
		if (cnt > 0) return JSONUtils.serialize(new Ret(1));
		return JSONUtils.serialize(new Ret(0));
	}
	
	@RequestMapping("/getCities")
	@ResponseBody
	public String geCities(){
		CityExample example = new CityExample();
    	example.setOrderByClause("code desc");
    	List<City> list = cityService.selectByExample(example);
    	return JSONUtils.serialize(list);
	}
	
	@RequestMapping("/fastLogin")
	public String fastLogin(String code,HttpSession session){
		UserGroup group = RoleUtil.getGroupByCode("cityAgent");
		
		if (group == null){
			return "sys/main";
		}

		City object = cityService.selectByPrimaryKey(code);
		if(object == null)return "sys/main";
		
		String id = code;
		String roleCode = object.getRoleCode();
		String areaCode = id;
		Role role = RoleUtil.getRoleByCode(roleCode);
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("areaCode", areaCode);
		map.put("id", id);
		map.put("account", object.getAccount());
		map.put("sys", group.getCode());
		map.put("sysname", group.getInfo());
		map.put("groupId", group.getId());
		map.put("groupCode", group.getCode());
		map.put("groupWelcome", StringUtils.isBlank(group.getUrl())?"/base/jsp/welcome.jsp":group.getUrl());
		map.put("roleId", role.getId());
		map.put("roleCode", role.getCode());
		map.put("rolename", role.getName());
		map.put("menu", JSONUtils.serialize(RoleUtil.initMenus(RoleUtil.getRightsByCode(group.getCode(),roleCode))));
		session.setAttribute(LoginMethod.ADMIN.getName(), map);
    	return "redirect:/manager/cityAgent";
	}
}
